﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Common;
using Domain;
namespace Web.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        //可以指点让某个用户访问,或者某个组的用户访问
        //[Authorize(Users = "?")]  
        public ActionResult Index()
        {
            //var name = User.Identity.Name;
            //string[] str = Roles.GetRolesForUser();

            //传递一个角色id
            DAO.BLL.B_Manager b_manager = new DAO.BLL.B_Manager();
            var model = b_manager.Get(Convert.ToInt32(User.Identity.Name));
            ViewData["manager"] = model;
            return View();
        }

        public ActionResult Login()
        {
            FormsAuthentication.SignOut();//安全退出
            return View();
        }

        public ActionResult Error() 
        {
            return View();
        }
        public ActionResult Install() 
        {
            return View();
        }

        #region ===================内部方法==========================
        /// <summary>
        /// 普通登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userpwd"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult login(string un, string pwd,string code,string returnUrl)
        {
            Common.Json json = new Common.Json();
            if (Common.Encrypt.md5(code.ToLower()) != WebHelper.GetSession("lsj_pic_code")) 
            {
                json.msg = "验证码不正确！";
                json.status = -1;
                return Json(json);
            }
            DAO.BLL.B_Manager b_manager = new DAO.BLL.B_Manager();
            if (ModelState.IsValid)
            {
                List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="user_name",value=un,searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="password",value=Common.Encrypt.md5(pwd),searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="is_lock",value="√",searchType=Common.EnumBase.SearchType.Eq}
                };
                var list_model = b_manager.GetList(st,null);
                if (list_model.Count>0)
                {
                    FormsAuthentication.SetAuthCookie(list_model[0].id.ToString(), true);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        json.returnUrl = returnUrl;
                    }
                    else
                    {
                        json.returnUrl = "/home/index";
                    }

                    DAO.BLL.B_Manager_log b_log = new DAO.BLL.B_Manager_log();
                    b_log.Add(list_model[0].id, Common.EnumBase.Authorize.登录.Description(), "manager", "用户登录", Utils.getIp());
                }
                else
                {
                    json.msg = "用户名或密码不正确！";
                    json.status = -1;
                }
            }

            return Json(json);
        }

        [HttpPost]
        public JsonResult Install(string key) 
        {
            Common.Json json = new Common.Json();
            json.msg = "安装成功!";
            json.returnUrl = "/home/login";
            if (key == "lsjznb")
            {
                try
                {
                    FormsAuthentication.SignOut();//安全退出
                    helper.Initialise ins = new helper.Initialise();
                    ins.Initializing();
                }
                catch 
                {
                    json.status = -1;
                    json.msg = "初始化失败!";
                }
            }
            else
            {
                json.status = -1;
                json.msg = "密钥不正确!";
            }
            return Json(json);
        }
        #endregion ===================内部方法==========================



        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }
       
    }
}
